<?php

namespace App\Http\Controllers\ChinaBank\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ChinaBank\AdminModel;

class LoginController extends Controller
{
    public function index(Request $request)
    {
        $admin  = $request->session()->get("admin");
        return self::adminview("index/index",['admin'=>$admin]);
    }


    public function login(Request $request){

        $name   = $request->input('username');
        $pwd    = $request->input('password');
        $error  = "";

        if($request->method() == 'POST')
        {
            $admin  = AdminModel::login($name,$pwd);
            if($admin){
                $request->session()->put('admin',$admin);
                $url = config('app.url').route("adminHome",[],false);
                return response()->redirectTo($url);
            }else{
                $error = "账号或密码错误";
            }
        }
        return self::adminview("index/login",["name"=>$name,"pwd"=>$pwd,"error"=>$error]);
    }

    public function logout(Request $request){

        $request->session()->forget("admin");
        $url = config('app.url').route("adminLogin",[],false);
        return response()->redirectTo($url);
    }


    public function welcome(Request $request){
        $admin = $request->session()->get('admin');
        return self::adminview("index/welcome",['admin'=>$admin]);
    }



}
